var WN_AGE_UTIL = (function() {
	
	/**
	 * 根据出生日期算出年龄
	 * @param strBirthday yyyy-mm-dd
	 * */ 
	function exchangeAge(strBirthday) {
		var diffAge = calDiffAge(strBirthday);
		return (typeof(diffAge.year)!='undefined'?(diffAge.year+'岁'):'')
//		var returnAge;
//		var strBirthdayArr = strBirthday.split("-");
//		var birthYear = strBirthdayArr[0];
//		var birthMonth = strBirthdayArr[1];
//		var birthDay = strBirthdayArr[2];
//
//		var d = new Date();
//		var nowYear = d.getFullYear();
//		var nowMonth = d.getMonth() + 1;
//		var nowDay = d.getDate();
//
//		if (nowYear == birthYear){
//			returnAge = 0;//同年 则为0岁 
//		} else {
//			var ageDiff = nowYear - birthYear;//年之差 
//			if (ageDiff > 0){
//				if (nowMonth == birthMonth) {
//					var dayDiff = nowDay - birthDay;//日之差 
//					if (dayDiff < 0) {
//						returnAge = ageDiff - 1;
//					} else {
//						returnAge = ageDiff;
//					}
//				} else {
//					var monthDiff = nowMonth - birthMonth;//月之差 
//					if (monthDiff < 0) {
//						returnAge = ageDiff - 1;
//					} else {
//						returnAge = ageDiff;
//					}
//				}
//			} else {
//				returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天 
//			}
//		}
//
//		return returnAge;//返回周岁年龄 
	}
	
	/**
	 * 计算年龄
	 * 年龄+月龄+日龄
	 * 5岁以上 只显示岁
	 * 1-5岁 显示岁+月
	 * 1岁以下 显示月+日
	 */
	function calDiffAge(birthdayTxt) {
		var strBirthdayArr = birthdayTxt.split("-");
		var birthYear = parseInt(strBirthdayArr[0]);
		var birthMonth = parseInt(strBirthdayArr[1]);
		var birthDay = parseInt(strBirthdayArr[2]);

		var d = new Date();
		var nowYear = d.getFullYear();
		var nowMonth = d.getMonth() + 1;
		var nowDay = d.getDate();
		
		var today = new Date().format('yyyy-MM-dd');
		var oneYearOld = new Date(birthYear + 1, birthMonth - 1, birthDay).format('yyyy-MM-dd');
		var fiveYearOld = new Date(birthYear + 5, birthMonth - 1, birthDay).format('yyyy-MM-dd');
//		console.log('oneYearOld== ' + oneYearOld);
//		console.log('fiveYearOld== ' + fiveYearOld);
		if (birthdayTxt > today) {
			return null;
		} else {
			var yearDiff = nowYear - birthYear;
			var monthDiff = nowMonth - birthMonth;
			var dayDiff = nowDay - birthDay;
			if (oneYearOld > today) { // 1岁以内
//				console.log('1岁以内');
				var monthDayArray = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
				
				monthDiff += yearDiff * 12; // 年diff转换成月diff
				if (dayDiff < 0) {
					monthDiff--;
					return {
						year: 0,
						month: monthDiff,
						day: monthDayArray[nowMonth - 1 - 1] + nowDay - birthDay
					}
//					return monthDiff + '个月' + (monthDayArray[nowMonth - 1 - 1] + nowDay - birthDay) + '天'; // 第一个（-1）是前一个月的日数，第二个（-1）是数据下标
				} else{
					return {
						year: 0,
						month: monthDiff,
						day: nowDay - birthDay
					}
//					return monthDiff + '个月' + (nowDay - birthDay) + '天';
				}
			} else {
				if (dayDiff < 0) {
					monthDiff--;
				}
				if (monthDiff < 0) {
					yearDiff--;
					monthDiff += 12; // 减下来的1年加入月中
				}
				
				if (fiveYearOld > today) { // 5岁以内
//					console.log('5岁以内');
					return {
						year: yearDiff,
						month: monthDiff
					}
//					return yearDiff + '岁' + monthDiff + '个月';
				} else {
//					console.log('5岁以外');
//					return yearDiff + '岁';
					return {
						year: yearDiff
					}
				}
			}
		}
	}
	
	
	/**
	 * 0岁时显示 m月n日
	 * 1-5岁时显示m岁n月
	 * 6岁以后显示m岁
	 */
	function formatAgeTxt2(year, month) {
		if (typeof(year)=='undefined' && typeof(month)=='undefined') {
			return null;
		} else {
			if (typeof(year)=='undefined') year = 0;
			if (typeof(month)=='undefined') month = 0;
		}
		
		
		if (year >= 5) {
			return (typeof(year)!='undefined'?(year+'岁'):''); 
		} else if (year >= 1) {
			return (typeof(year)!='undefined'?(year+'岁'):'') + (typeof(month)!='undefined'?(month+'个月'):'');
		} else {
			return /*(typeof(year)!='undefined'?(year+'岁'):'') + */(typeof(month)!='undefined'?(month+'个月'):'');
		}
	}
	
	/**
	 * 0岁时显示 m月n日
	 * 1-5岁时显示m岁n月
	 * 6岁以后显示m岁
	 */
	function formatAgeTxt(birthdayTxt) {
		var diffAge = calDiffAge(birthdayTxt);
		if (diffAge.year >= 5) {
			return (typeof(diffAge.year)!='undefined'?(diffAge.year+'岁'):''); 
		} else if (diffAge.year >= 1) {
			return (typeof(diffAge.year)!='undefined'?(diffAge.year+'岁'):'') + (typeof(diffAge.month)!='undefined'?(diffAge.month+'个月'):'');
		} else {
			return /*(typeof(diffAge.year)!='undefined'?(diffAge.year+'岁'):'') + */(typeof(diffAge.month)!='undefined'?(diffAge.month+'个月'):'');
		}
//		var strBirthdayArr = birthdayTxt.split("-");
//		var birthYear = parseInt(strBirthdayArr[0]);
//		var birthMonth = parseInt(strBirthdayArr[1]);
//		var birthDay = parseInt(strBirthdayArr[2]);
//
//		var d = new Date();
//		var nowYear = d.getFullYear();
//		var nowMonth = d.getMonth() + 1;
//		var nowDay = d.getDate();
//		
//		var today = new Date().format('yyyy-MM-dd');
//		var oneYearOld = new Date(birthYear + 1, birthMonth - 1, birthDay).format('yyyy-MM-dd');
//		var fiveYearOld = new Date(birthYear + 5, birthMonth - 1, birthDay).format('yyyy-MM-dd');
//		console.log('oneYearOld== ' + oneYearOld);
//		console.log('fiveYearOld== ' + fiveYearOld);
//		if (birthdayTxt > today) {
//			return '生日异常';
//		} else {
//			var yearDiff = nowYear - birthYear;
//			var monthDiff = nowMonth - birthMonth;
//			var dayDiff = nowDay - birthDay;
//			if (oneYearOld > today) { // 1岁以内
//				console.log('1岁以内');
//				var monthDayArray = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//				
//				monthDiff += yearDiff * 12; // 年diff转换成月diff
//				if (dayDiff < 0) {
//					monthDiff--;
//					return monthDiff + '个月' + (monthDayArray[nowMonth - 1 - 1] + nowDay - birthDay) + '天'; // 第一个（-1）是前一个月的日数，第二个（-1）是数据下标
//				} else{
//					return monthDiff + '个月' + (nowDay - birthDay) + '天';
//				}
//			} else {
//				if (dayDiff < 0) {
//					monthDiff--;
//				}
//				if (monthDiff < 0) {
//					yearDiff--;
//					monthDiff += 12; // 减下来的1年加入月中
//				}
//				
//				if (fiveYearOld > today) { // 5岁以内
//					console.log('5岁以内');
//					return yearDiff + '岁' + monthDiff + '个月';
//				} else {
//					console.log('5岁以外');
//					return yearDiff + '岁';
//				}
//			}
//		}
	}
	
	/**
	 * 根据年龄，月龄 计算生日
	 */
	function formatBirthdayTxt(diffYear, diffMonth) {
//		console.log('diffYear==' + diffYear)
//		console.log('diffMonth==' + diffMonth)
		
		var d = new Date();
		var nowYear = d.getFullYear();
		var nowMonth = d.getMonth() + 1;
		var nowDay = d.getDate();
		
		if (typeof(diffYear) == 'undefined') diffYear = 0;
		if (typeof(diffMonth) == 'undefined') diffMonth = 0;
		
		nowYear -= diffYear;
		nowMonth -= diffMonth;
		while (nowMonth <= 0) {
			nowYear--;
			nowMonth += 12;
		}
		return nowYear + '-' + (nowMonth>=10?nowMonth:'0'+nowMonth) + '-' + (nowDay>=10?nowDay:'0'+nowDay);
	}
	
	return {
		calDiffAge: calDiffAge,
		exchangeAge: exchangeAge,
		formatAgeTxt: formatAgeTxt,
		formatAgeTxt2: formatAgeTxt2,
		formatBirthdayTxt: formatBirthdayTxt
	}
})();